var baseConfig = require("../../baseConfig");
// var utils = require("../../utils/utils");
Page({
  data: {
    layer1Popup: false,
    toView: 'green',
    imgList: []
  },
  onLoad: function (options) {
    // console.log(options);
    this.setData({
      testIcon: options.test_icon,
      testName: options.test_name,
      appId: options.appid,
      appName: options.name,
      goodsId: options.goodsId,
      logo: options.logo,
    })
    this.getImg()
  },
  getImg() {
    // console.log('get_goods_img', this.data.goodsId);
    wx.request({
      url: baseConfig.baseUrl + 'get_goods_img',
      method: "GET",
      data: {
        goodsId: this.data.goodsId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: res => {
        // console.log(res.data.data);
        // let obj = res.data.data
        // obj.forEach( function(item){
        //   if(item == null) {
        //     var obj1 = obj[0]
        //     console.log(obj1);
        //   }
        // })
        this.setData({
          imgList: res.data.data
        })
      }
    })
  },
  
  download(e) {
    console.log(e.currentTarget.dataset.url);
    let url = e.currentTarget.dataset.url;
    wx.downloadFile({
      url: url,
      success: res => {
        if (res.statusCode === 200) {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.showToast({
                title: '保存图片成功！',
              })
            },
            fail: () => {
              wx.showToast({
                title: '保存图片失败！',
              })
            }
          })
        }
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  tuiGuang(e) {
    console.log('111', e.currentTarget.dataset);
    let goodsId = e.currentTarget.dataset.goodsid;
    console.log(goodsId);
    let appid = e.currentTarget.dataset.appid;
    console.log(appid);
    wx.request({
      url: baseConfig.baseUrl + `qrcode/tt/${appid}`,
      method: "GET",
      data: {
        is_weixin: 0,
        goodsId: goodsId
      },
      header: {
        "Authorization": wx.getStorageSync('token'),
        "UID": wx.getStorageSync('userid'),
        'Content-Type': 'application/json'
      },
      success: res => {
        this.setData({
          layer1Popup: true,
          imgUrl: res.data.data
        })
      }
    })
  },
  douYinCloseClick() {
    this.setData({
      layer1Popup: false,
    })
  },
  jiaochengClick() {
    wx.navigateTo({
      url: "../problem/problem"
    })
  },
  saveImage(filePath) {
    var aa = wx.getFileSystemManager();
    aa.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/test.png',
      data: this.data.imgUrl,
      encoding: 'base64',
      success: () => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/test.png',
          success: () => {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: (err) => {
            console.log(err)
          }
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  saveImageToPhotos(filePath) {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => {
              this.saveImage(filePath)
              this.isGetSet = true
            },
            fail: () => {
              this.isGetSet = false
            }
          })
        } else {
          this.isGetSet = true
          this.saveImage(filePath)
        }
      },
      fail(res) {
        console.log(res);
      }
    })
  },
})